home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12018 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  94 lines

  1. Path: research.nokia.com!news
  2. From: Johan Wikman <johan.wikman@research.nokia.com>
  3. Newsgroups: comp.lang.c++,comp.object
  4. Subject: Re: Style question: OOD & initialisation
  5. Date: Sun, 17 Mar 1996 19:35:08 +0200
  6. Organization: Nokia Research Center
  7. Message-ID: <314C4D4C.51B4@research.nokia.com>
  8. References: <MARTING.96Mar13154718@igly.jtec.com.au> <RMARTIN.96Mar14120844@rcm.oma.com>
  9. NNTP-Posting-Host: nrcpb-127.research.nokia.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Robert C. Martin wrote:
  16. > In article <MARTING.96Mar13154718@igly.jtec.com.au> marting@jtec.com.au (Martin Gregory) writes:
  17. >    main()
  18. >    {
  19. >      Controller TheController;
  20. >    }
  21. >    However, is this concept of a constructor that doesn't exit till the
  22. >    end of time a bit of a worry?  (I think it is at least valid.)
  23. >    Would this type of thing be preferrable:
  24. >    main()
  25. >    {
  26. >      Controller TheController;
  27. >      TheController.Go();
  28. >    }
  29. >    If so, why?
  30. > Because it allows the following:
  31. > main()
  32. > {
  33. >   Controller c1(1), c2(2);
  34. >   if (/* some test */)
  35. >     c1.go();
  36. >   else
  37. >     c2.go();
  38. > };
  39.  
  40. You could just as well have:
  41.  
  42.   main()
  43.   {
  44.     if (/* some test* /)
  45.       {
  46.         Controller c1(1);
  47.       }
  48.     else
  49.       {
  50.         Controller c2(2);
  51.       }
  52.   }
  53.  
  54. in which case you would also only create *one* Controller object (which
  55. is what you want) instead of two.
  56.  
  57. I prefer the go-version of three reasons:
  58.  
  59. 1) I think a constructor should only initialize an object and do
  60.    nothing else.
  61.  
  62. 2) If the constructor does the lot, it effectively means that you can
  63.    not derive from the class.
  64.  
  65. 3) It allows you to write
  66.  
  67.    int main()
  68.    {
  69.      Controller c;
  70.  
  71.      return c.go();
  72.    }
  73.  
  74.    which I think is nice.
  75.  
  76. > --
  77. > Robert Martin       | Design Consulting   | Training courses offered:
  78. > Object Mentor Assoc.| rmartin@oma.com     |   OOA/D, C++, Advanced OO
  79. > 14619 N. Somerset Cr| Tel: (847) 918-1004 |   Mgt. Overview of OOT
  80. > Green Oaks IL 60048 | Fax: (847) 918-1023 | http://www.oma.com
  81.  
  82. -- 
  83. johan.wikman@research.nokia.com
  84.